﻿let currentRotation = 0;
const carousel = document.querySelector('.carousel');
const prevBtn = document.getElementById('prev');
const nextBtn = document.getElementById('next');

function rotateCarousel(direction) {
    currentRotation += direction * 45;
    carousel.style.transform = `rotateY(${currentRotation}deg)`;
}

prevBtn.addEventListener('click', () => {
    rotateCarousel(1);
});

nextBtn.addEventListener('click', () => {
    rotateCarousel(-1);
});

// 添加触摸滑动支持
let touchStartX = 0;
let touchEndX = 0;

document.addEventListener('touchstart', (e) => {
    touchStartX = e.touches[0].clientX;
});

document.addEventListener('touchend', (e) => {
    touchEndX = e.changedTouches[0].clientX;
    handleSwipe();
});

function handleSwipe() {
    const swipeDistance = touchEndX - touchStartX;
    if (Math.abs(swipeDistance) > 50) {
        if (swipeDistance > 0) {
            rotateCarousel(1);
        } else {
            rotateCarousel(-1);
        }
    }
}

let isDragging = false;
let startX;
let startRotation;

carousel.addEventListener('mousedown', (e) => {
    isDragging = true;
    startX = e.clientX;
    startRotation = currentRotation;

    // 添加鼠标移动事件监听
    document.addEventListener('mousemove', handleMouseMove);
    document.addEventListener('mouseup', () => {
        isDragging = false;
        document.removeEventListener('mousemove', handleMouseMove);
    }, { once: true });
});

function handleMouseMove(e) {
    if (!isDragging) return;

    const deltaX = e.clientX - startX;
    const sensitivity = 0.5; // 调整灵敏度
    const newRotation = startRotation + (deltaX * sensitivity);

    carousel.style.transform = `rotateY(${newRotation}deg)`;
    currentRotation = newRotation;
}

// 添加自动旋转功能
let autoRotate = true;
const autoRotateSpeed = 0.5;

// 首先声明音乐相关变量
const bgMusic = document.getElementById('bgMusic');
const musicToggle = document.getElementById('musicToggle');
let isMusicPlaying = false;

// 音乐控制函数
function toggleMusic() {
    if (isMusicPlaying) {
        bgMusic.pause();
        musicToggle.classList.remove('playing');
    } else {
        bgMusic.play();
        musicToggle.classList.add('playing');
    }
    isMusicPlaying = !isMusicPlaying;
}

// 添加点击事件监听
musicToggle.addEventListener('click', toggleMusic);

// 修改自动旋转函数
function autoRotateCarousel() {
    if (autoRotate && !isDragging) {
        currentRotation -= autoRotateSpeed;
        carousel.style.transform = `rotateY(${currentRotation}deg)`;

        // 尝试自动播放音乐
        try {
            if (!isMusicPlaying) {
                bgMusic.play().catch(error => {
                    console.log('自动播放失败:', error);
                });
                musicToggle.classList.add('playing');
                isMusicPlaying = true;
            }
        } catch (error) {
            console.log('音乐播放出错:', error);
        }
    }
    requestAnimationFrame(autoRotateCarousel);
}

// 开始自动旋转
autoRotateCarousel();

// 鼠标悬停时暂停自动旋转
carousel.addEventListener('mouseenter', () => {
    autoRotate = false;
    if (isMusicPlaying) {
        bgMusic.pause();
        musicToggle.classList.remove('playing');
        isMusicPlaying = false;
    }
});

carousel.addEventListener('mouseleave', () => {
    autoRotate = true;
});

// 处理评论展开/折叠功能
document.querySelectorAll('.comment-toggle').forEach(button => {
    button.addEventListener('click', function () {
        const commentsSection = this.closest('.post').querySelector('.comments-section');
        commentsSection.classList.toggle('hidden');
    });
});

// 处理图片上传和预览
document.getElementById('image-upload').addEventListener('change', function (e) {
    const preview = document.querySelector('.image-preview');
    preview.innerHTML = '';

    [...e.target.files].forEach(file => {
        if (file.type.startsWith('image/')) {
            const reader = new FileReader();
            reader.onload = function (e) {
                const img = document.createElement('img');
                img.src = e.target.result;
                preview.appendChild(img);
            }
            reader.readAsDataURL(file);
        }
    });
});

// 处理评论提交
document.querySelectorAll('.comment-form').forEach(form => {
    form.addEventListener('submit', function (e) {
        e.preventDefault();
        const input = this.querySelector('.comment-input');
        const commentText = input.value.trim();

        if (commentText) {
            const commentsList = this.closest('.comments-section').querySelector('.comments-list');
            const newComment = createCommentElement(commentText);
            commentsList.insertBefore(newComment, commentsList.firstChild);
            input.value = '';
        }
    });
});

// 创建新评论元素
function createCommentElement(text) {
    const comment = document.createElement('div');
    comment.className = 'comment';
    comment.innerHTML = `
        <img src="avatar.jpg" alt="评论者头像" class="comment-avatar">
        <div class="comment-content">
            <div class="comment-header">
                <span class="comment-author">当前用户</span>
                <span class="comment-time">刚刚</span>
            </div>
            <p class="comment-text">${text}</p>
        </div>
    `;
    return comment;
}

// 处理点赞功能
document.querySelectorAll('.action-btn').forEach(btn => {
    if (btn.innerHTML.includes('点赞')) {
        btn.addEventListener('click', function () {
            const icon = this.querySelector('i');
            if (icon.classList.contains('fas')) {
                icon.classList.replace('fas', 'far');
                this.style.color = '#65676b';
            } else {
                icon.classList.replace('far', 'fas');
                this.style.color = '#1877f2';
            }
        });
    }
});